﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace Liang.Device.Model.Camera
{
    /// <summary>
    /// 艾普视达摄像头
    /// </summary>
    public class AipStar : CameraBase
    {
        /// <summary>
        /// 控制句柄
        /// <para>不开视频预览的实时抓拍，用这个句柄</para>
        /// </summary>
        [ScriptIgnore]
        public IntPtr DevControlHandle { get; set; }

        /// <summary>
        /// 播放句柄（用于预览）
        /// </summary>
        [ScriptIgnore]
        public IntPtr DevViewHandle { get; set; }

        /// <summary>
        /// 码流（0：主码流 1：次码流）
        /// </summary>
        public byte Stream { get; set; }

        /// <summary>
        /// 分辨率
        /// </summary>
        public byte Resolution { get; set; }

        /// <summary>
        /// 色彩模式
        /// </summary>
        public byte ColorTransMode { get; set; }

        /// <summary>
        /// 快门模式
        /// </summary>
        public byte ShutterSpeed { get; set; }

        /// <summary>
        /// 仅抓拍，不预览
        /// </summary>
        public bool OpenForCaptureOnly { get; set; }

        /// <summary>
        /// 增益
        /// </summary>
        public byte Agc { get; set; }
    }

    #region 控制命令

    /// <summary>
    /// 控制命令
    /// </summary>
    public static class ControlCommand
    {
        /// <summary>
        /// 视频输入配置
        /// </summary>
        public static readonly int TMCC_MAJOR_CMD_VIDEOINCFG = 0x116;

        /// <summary>
        /// 输入配置
        /// </summary>
        public static readonly int TMCC_MINOR_CMD_VIDEOIN = 0x00;
    }

    #endregion

    #region 视频设置参数

    /// <summary>
    /// 视频输入配置
    /// <para>主命令：TMCC_MAJOR_CMD_VIDEOINCFG</para>
    /// <para>子命令：TMCC_MINOR_CMD_VIDEOIN</para>
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct tmVideoInCfg_t
    {
        /// <summary>
        /// 结构体大小
        /// </summary>
        public uint dwSize;

        /// <summary>
        /// <para>0-auto</para>
        /// <para>1-50Hz</para>
        /// <para>2-60Hz</para>
        /// </summary>
        public byte byAntiFlickerMode;

        /// <summary>
        /// <para>0:VIDEO_COLOR_STYLE_FOR_PC</para>
        /// <para>1:VIDEO_COLOR_STYLE_FOR_TV</para>
        /// </summary>
        public byte byVideoColorStyle;

        /// <summary>
        /// <para>视频输入旋转</para>
        /// <para>0-normal</para>
        /// <para>1-180</para>
        /// <para>2-90</para>
        /// <para>3-270</para>
        /// <para>4-vflip</para>
        /// <para>5-hflip</para>
        /// </summary>
        public byte byRotaeAngle180;

        /// <summary>
        /// 彩转黑模式
        /// <para>0-自动</para>
        /// <para>1-彩色</para>
        /// <para>2-黑白</para>
        /// </summary>
        public byte byColorTransMode;

        /// <summary>
        /// 快门模式
        /// <para>取值范围:</para>
        /// <para>0=1/25,1=1/30,2=1/50,3=1/60,4=1/100,5=1/120,6=1/240,7=1/480,8=1/960,9=1/1024</para>
        /// <para>10=自动,11-1/4,12-1/8,13-1/15,14-1/180,15-1/2000,16-1/4000,17-1/10000</para>
        /// </summary>
        public byte byShutterSpeed;

        /// <summary>
        /// AGC增益
        /// <para>0=42dB,32=36dB,64=30dB,96=24dB,128=18dB,160=12dB,192=6dB,224-自动,225-48dB,226-54dB,227-60dB</para>
        /// </summary>
        public byte byAgc;

        /// <summary>
        /// 红外切换模式
        /// <para>0-自动切换模式</para>
        /// <para>1-定时切换</para>
        /// <para>2-使用报警输入切换</para>
        /// <para>0xFF-手动控制模式</para>
        /// </summary>
        public byte byIRShutMode;

        /// <summary>
        /// 光圈曝光
        /// <para>0-最大光圈，100-最小光圈，0xFF-手动</para>
        /// </summary>
        public byte byExposure;

        /// <summary>
        /// 红外打开时间
        /// </summary>
        public byte byIRStartHour;

        /// <summary>
        /// 红外打开时间
        /// </summary>
        public byte byIRStartMin;

        /// <summary>
        /// 红外关闭时间
        /// </summary>
        public byte byIRStopHour;

        /// <summary>
        /// 红外关闭时间
        /// </summary>
        public byte byIRStopMin;

        /// <summary>
        /// <para>0x00-可编程自动曝光,0x10-可编程自动曝光R1,0x20-可编程自动曝光R2</para>
        /// <para>0x01-可编程自动曝光+,0x11-可编程自动曝光R+1,0x21-可编程自动曝光R+2,0x31-可编程自动曝光R+3</para>
        /// <para>手动选择:模式0x07-以快门优先，0x08-曝光优先，0x09-自动增益优先</para>
        /// </summary>
        public byte byModeSwitch;

        /// <summary>
        /// 白平衡控制
        /// <para>0-关闭,1-自动,2-白炽灯,3-D4000,4-D5000,5-日光,6-阴天,7-闪光灯,8-荧光的,9-荧光的H,10-水下,0xFF-手动</para>
        /// </summary>
        public byte byWhiteBalance;

        /// <summary>
        /// 宽动态模式
        /// <para>0-关,1-自动,2-内部模式1X,3-内部模式2X,4-内部模式3X,5-内部模式4X, 0xFF-默认</para>
        /// </summary>
        public byte byWdr;

        /// <summary>
        /// 背光补偿
        /// <para>0-关，1-开</para>
        /// </summary>
        public byte byBlc;

        /// <summary>
        /// 白平衡手动值R（0~16383）
        /// </summary>
        ushort nWhiteBalanceR;

        /// <summary>
        /// 白平衡手动值B（0~16383）
        /// </summary>
        ushort nWhiteBalanceB;

        /// <summary>
        /// 3D降噪（0~255）
        /// </summary>
        public byte byMctfStrength;

        /// <summary>
        /// 滤光片类型
        /// <para>0-持续正向，1-持续反向，2-瞬间正向，3-瞬间反向</para>
        /// </summary>
        public byte byIRType;

        /// <summary>
        /// 切换滤光片是否控制报警输出控制红外灯
        /// <para>注：报警输出需连接红外灯</para>
        /// </summary>
        public byte byIRCutTriggerAlarmOut;

        /// <summary>
        /// 滤光片切换时间
        /// <para>0-500（默认值）</para> 
        /// <para>1-100</para> 
        /// <para>2-200</para> 
        /// <para>3-300</para>
        /// </summary>
        public byte byIRCutTime;

        /// <summary>
        /// 曝光水平( 10 ~ 200 )
        /// </summary>
        public byte byExposureLevel;

        /// <summary>
        /// 黑白模式自动切换的亮度阀值（只有在自动模式有效）
        /// </summary>
        public byte byColorTransMin;

        /// <summary>
        /// 黑白模式自动切换的亮度阀值（只有在自动模式有效）
        /// </summary>
        public byte byColorTransMax;

        /// <summary>
        /// 降噪级别
        /// <para>自动降噪：0xFF</para>
        /// <para>二级降噪：0,1,2,3,4,5,6,7,8</para>
        /// </summary>
        public byte byNoiseFilter;

        /// <summary>
        /// 是否强制降噪
        /// <para>0：byNoiseFilter为下限值</para>
        /// <para>1：byNoiseFilter为固定值</para>
        /// </summary>
        public byte byForceNoiseFilter;

        /// <summary>
        /// 曝光模式
        /// <para>0-spot,1-center,2-average,3-custom</para>
        /// </summary>
        public byte byAeMeteringMode;

        /// <summary>
        /// 宽动态模式
        /// <para>0-数字宽动态</para>
        /// <para>1-硬件宽动态</para>
        /// </summary>
        public byte byWdrMode;

        /// <summary>
        /// 报警输入联动红外的报警号(当报警输入>1时生效)
        /// <para>0：内部决定的报警输入（默认值）</para>
        /// <para>1：第一个报警输入</para>
        /// <para>2：第二个报警输入</para>
        /// </summary>
        public byte byIRShutAlarmIn;

        /// <summary>
        /// 使用自动对比度
        /// </summary>
        public byte byAutoContrast;

        /// <summary>
        /// 强光抑制功能
        /// </summary>
        public byte byLightInhibitionEn;

        /// <summary>
        /// 强制设置帧率
        /// <para>0-自动, 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-10, 8-12, 9-13, 10-14, 11-15, 12-20, 13-24, 14-25, 15-30, 16-50, 17-60, 18-120</para>
        /// <para>19-29.75, 20-59.94, 21-23.976, 22-12.5, 23-6.25, 24-3.125, 25-7.5, 26-3.75</para>
        /// </summary>
        public byte byVinFrameRate;

        /// <summary>
        /// 保留
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]
        public byte[] byTemp;

        /// <summary>
        /// 黑白模式自动切换的增益阀值，只有在自动模式有效
        /// </summary>
        public byte byAgcTransMin;

        /// <summary>
        /// 黑白模式自动切换的增益阀值，只有在自动模式有效
        /// </summary>
        public byte byAgcTransMax;

        /// <summary>
        /// 快门取值最大值（单位：1/x）
        /// <para>默认值：0</para>
        /// </summary>
        public ushort nMaxShutterSpeed;

        /// <summary>
        /// AGC增益最大值（单位：db）
        /// <para>默认值：0</para>
        /// </summary>
        public ushort nMaxAgc;

        /// <summary>
        /// 曝光区域
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 96)]
        public byte[] byAeMeteringData;

        /// <summary>
        /// HDR曝光水平( 10 ~ 200 )
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] byExposureLevelHdr;

        /// <summary>
        /// HDR快门低限
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public ushort[] nMinShutterSpeedHdr;

        /// <summary>
        /// HDR快门高限
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public ushort[] nMaxShutterSpeedHdr;

        /// <summary>
        /// HDR最大增益
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] byMaxAgcHdr;

        /// <summary>
        /// 曝光区域
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4 * 96)]
        public byte[,] byAeMeteringDataHdr;
    }

    #endregion
}
